package com.mogist.hqc.module.common.ui

import android.app.Dialog
import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.Gravity
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import android.widget.TextView
import butterknife.OnClick
import butterknife.Optional
import com.chad.library.adapter.base.BaseQuickAdapter
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.entitys.BaseModel
import com.mogist.hqc.entitys.response.CommentListVo
import com.mogist.hqc.module.common.adapter.CommentAdapter
import com.mogist.hqc.module.common.contract.CommentListContract
import com.mogist.hqc.module.common.presenter.CommentListPresenter
import com.mogist.hqc.mvp.HttpDto
import com.mogist.hqc.util.Constant
import kotlinx.android.synthetic.main.activity_comment_list.*
import talex.zsw.basecore.util.KeyboardTool
import talex.zsw.basecore.util.RegTool
import talex.zsw.basecore.view.other.RxToast
import talex.zsw.basecore.view.other.swipetoloadlayout.OnLoadMoreListener
import talex.zsw.basecore.view.other.swipetoloadlayout.OnRefreshListener
import talex.zsw.basecore.view.recyleview.DividerItemDecoration
import talex.zsw.basecore.view.recyleview.SampleFooter
import talex.zsw.basecore.view.recyleview.SampleHeader

/**
 * 作用：产品,地点,线路的 评论列表
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class CommentListActivity : BaseMVPActivity<CommentListContract.Presenter>(), CommentListContract.View, OnRefreshListener, OnLoadMoreListener
{
    private var id = ""
    private var type = 0 // 0:产品
    override fun initArgs(intent: Intent)
    {
        intent.getStringExtra("id")
                ?.let {
                    id = it
                }
        type = intent.getIntExtra("type", 0)
    }

    override fun initView()
    {
        setContentView(R.layout.activity_comment_list)
        mPresenter = CommentListPresenter(this)

        getWindow().setLayout(-1, -1)
    }

    @Optional
    @OnClick(R.id.mFLBottom, R.id.mTvMask, R.id.mIvClose)
    fun onViewClicked(view: View)
    {
        if (!isFastClick)
        {
            when (view.id)
            {
                R.id.mFLBottom              ->
                {
                    showCommentDialog()
                }
                R.id.mTvMask, R.id.mIvClose ->
                {
                    finish()
                    overridePendingTransition(0, R.anim.activity_bottom_close)
                }
            }
        }
    }

    override fun initData()
    {
        initRecyclerView()
        if (type == 0)
        {
            mFLBottom.visibility = View.GONE
        }
        getData(false)
    }

    private val adapter = CommentAdapter()

    private fun initRecyclerView()
    {
        adapter.emptyView =
                SampleHeader(this@CommentListActivity, swipe_target.width, swipe_target.height)
        adapter.isUseEmpty(false)
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN)
        swipe_target.addItemDecoration(DividerItemDecoration(DividerItemDecoration.VERTICAL_LIST,
                                                             R.color.line))
        swipe_target.layoutManager =
                LinearLayoutManager(this@CommentListActivity) as RecyclerView.LayoutManager?
        swipe_target.adapter = adapter
        // mRecyclerView.isNestedScrollingEnabled = false
        mSwipeToLoadLayout.setOnRefreshListener(this)
        mSwipeToLoadLayout.setOnLoadMoreListener(this)
        mSwipeToLoadLayout.isLoadMoreEnabled = true
        mSwipeToLoadLayout.isRefreshEnabled = true
    }

    // --------------- 玩家产品评论列表 ---------------
    override fun bindComment(data: CommentListVo)
    {
        adapter.isUseEmpty(true)
        if (data.list != null && data.list.isNotEmpty())
        {
            if (page == 1)
            {
                adapter.replaceData(data.list)
            }
            else
            {
                adapter.addData(data.list)
            }
            if (data.pages == page)
            {
                sampleFooter
                        ?: let {
                            sampleFooter = SampleFooter(this@CommentListActivity)
                        }
                adapter.setFooterView(sampleFooter)
                mSwipeToLoadLayout.isLoadMoreEnabled = false
            }
            page++
        }
        else
        {
            if (page == 1)
            {
                adapter.replaceData(arrayListOf())
                mSwipeToLoadLayout.isLoadMoreEnabled = false
            }
        }
    }

    // --------------- 数据获取 ---------------
    override fun onDestroy()
    {
        super.onDestroy()
        adapter.removeAllFooterView()
        sampleFooter = null
    }

    var sampleFooter: SampleFooter? = null
    private var page = 1
    override fun getData(silence: Boolean)
    {
        val body = BaseModel(true)
        when (type)
        {
            0 ->
            {
                body.pdtId = id
                body.page = page.toString()
                mPresenter.getData(HttpDto(Constant.PRODUCT_COMMENT,
                                           body,
                                           silence).setType(HttpDto.GET).setTryAgain(true).setErrorTitle(
                        "获取评论列表失败"))
            }
            1 ->
            {
                body.projectId = id
                body.page = page.toString()
                mPresenter.getData(HttpDto(Constant.TYPE_COMMENT_LIST, body, silence).setType(
                        HttpDto.GET).setTryAgain(true).setErrorTitle("获取评论列表失败"))
            }
        }
    }

    override fun disLoading()
    {
        mSwipeToLoadLayout.isLoadingMore = false
        mSwipeToLoadLayout.isRefreshing = false
    }

    override fun onRefresh()
    {
        adapter.removeAllFooterView()
        page = 1
        mSwipeToLoadLayout.isLoadMoreEnabled = true
        mSwipeToLoadLayout.isRefreshEnabled = true
        getData(true)
    }

    override fun onLoadMore()
    {
        getData(true)
    }

    // --------------- 评论 ---------------
    private var dialog: Dialog? = null
    private var dEditText: EditText? = null
    private var commentStr = ""
    private fun showCommentDialog()
    {
        dialog
                ?: let {
                    dialog = Dialog(this@CommentListActivity, R.style.dialog)
                    dialog?.setContentView(R.layout.dialog_comment)
                    dialog?.setCanceledOnTouchOutside(true)
                    val display = window.windowManager.defaultDisplay
                    val layoutParams: WindowManager.LayoutParams = dialog?.window!!.attributes
                    layoutParams.width = (display.width * 1).toInt()
                    // layoutParams.height = (display.height * 0.8f).toInt()
                    layoutParams.gravity = Gravity.BOTTOM
                    if (layoutParams.gravity == Gravity.BOTTOM)
                    {
                        layoutParams.y = 0
                    }
                    dialog?.window!!.attributes = layoutParams

                    dEditText = dialog?.findViewById<EditText>(R.id.dEditText)
                    dialog?.findViewById<TextView>(R.id.dTvSure)
                            ?.setOnClickListener {
                                commentStr =
                                        dEditText!!.text.toString()
                                                .trim()
                                if (RegTool.isEmpty(commentStr))
                                {
                                    RxToast.warning("请输入评论信息")
                                }
                                else
                                {
                                    // 项目评论
                                    val body = BaseModel(true)
                                    body.pdtId = id
                                    body.content = commentStr
                                    mPresenter.getData(HttpDto(Constant.TYPE_COMMENT_ADD,
                                                               body).setErrorTitle("发表评论失败"))
                                }
                            }
                    dialog?.findViewById<TextView>(R.id.dTvCancel)
                            ?.setOnClickListener {
                                dialog?.dismiss()
                            }
                }
        dEditText?.setText("")
        KeyboardTool.showSoftInput(this@CommentListActivity, dEditText)
        dialog?.show()
    }

    override fun commentSuccess()
    {
        RxToast.success("评论成功")
        dialog?.dismiss()
        finish()
    }

}